﻿using WebSelfHost.Entity.Sys;
using WebSelfHost.Logic.Sys;
using WebSelfHost.Web.Filters;
using WebSelfHost.Utility.Core;
using WebSelfHost.Utility.ResponseModels;

using Microsoft.AspNetCore.Mvc;
using WebSelfHost.Entity.WebRequestParms.Sys.Log; 
using System.IO; 
using WebSelfHost.Utility.Files; 
using WebSelfHost.Utility.Extension;
using WebSelfHost.Utility.Other;

#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http;
#endif
namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class LogController : BaseController
    {
        private SysLogLogic logic;
        public LogController()
        {
            logic = new SysLogLogic();
        }
        /// <summary>
        /// 操作日志主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/log/operateIndex"), AuthorizeChecked]
        public ActionResult OperateIndex()
        {
            return Html("/Sys/Log/OperateIndex");
        }

        /// <summary>
        /// 运行日志主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/log/runningIndex"), AuthorizeChecked]
        public ActionResult RunningIndex()
        {
            return Html("/Sys/Log/RunningIndex");
        }

        /// <summary>
        /// 错误日志主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/log/errorIndex"), AuthorizeChecked]
        public ActionResult ErrorIndex()
        {
            return Html("/Sys/Log/ErrorIndex");
        }

        [HttpPost, Route("system/log/export"), AuthorizeChecked]
        public ActionResult Export(LogIndexParms parms)
        {
            string filePath = MyEnvironment.RootPath(UUID.StrSnowId + ".zip");
            string basePath = string.Empty;
            string fileDownloadName = string.Empty;
            //读取配置文件，得到三个日志文件夹
            if (parms.type == "Error")
            {
                basePath = GlobalValue.Config.LogBasePath + GlobalValue.Config.ErrorLogPath;
                fileDownloadName = "ErrorLog.zip";
            }
            else if (parms.type == "Operate")
            {
                basePath = GlobalValue.Config.LogBasePath + GlobalValue.Config.OperateLogPath;
                fileDownloadName = "OperateLog.zip";
            }
            else
            {
                basePath = GlobalValue.Config.LogBasePath + GlobalValue.Config.InfoLogPath;
                fileDownloadName = "InfoLog.zip";
            }
            string baseDirPath = MyEnvironment.RootPath(basePath);
            if (!Directory.Exists(baseDirPath))
            {
                return null;
            }
            try
            {
                //压缩
                ZipHelper.PackFiles(filePath, baseDirPath);
                byte[] bytes = FileUtil.FileToBytes(filePath);
                FileUtil.Delete(filePath);
                return File(bytes, "application/zip", fileDownloadName);
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 根据keyWord获取日志信息
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/log/index"), LoginChecked]
        public ActionResult Index(LogIndexParms parms)
        {
            int totalCount = 0;
            var pageData = logic.GetList(parms.pageIndex, parms.pageSize, parms.type, parms.index, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysLog>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 删除日志界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/log/delete"), AuthorizeChecked]
        public ActionResult Delete()
        {
            return Html("/Sys/Log/Delete");
        }

        /// <summary>
        /// 根据选择的删除方法执行删除
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/log/delete"), AuthorizeChecked]
        public ActionResult Delete(LogDeleteParms parms)
        {
            logic.Delete(parms.type, parms.index);
            return Success();
        }

        /// <summary>
        /// 根据keyWord获取日志信息
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/log/index")]
        public ActionResult AppIndex(LogIndexParms parms)
        {
            int totalCount = 0;
            var pageData = logic.GetList(parms.pageIndex, parms.pageSize, parms.type, parms.index, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysLog>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 根据选择的删除方法执行删除
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/log/delete")]
        public ActionResult AppDelete(LogDeleteParms parms)
        {
            logic.Delete(parms.type, parms.index);
            return Success();
        }

        [HttpPost, Route("app/system/log/export")]
        public ActionResult AppExport(LogIndexParms parms)
        {
            string filePath = MyEnvironment.RootPath(UUID.StrSnowId + ".zip");
            string basePath = string.Empty;
            string fileDownloadName = string.Empty;
            //读取配置文件，得到三个日志文件夹
            if (parms.type == "Error")
            {
                basePath = GlobalValue.Config.LogBasePath + GlobalValue.Config.ErrorLogPath;
                fileDownloadName = "ErrorLog.zip";
            }
            else if (parms.type == "Operate")
            {
                basePath = GlobalValue.Config.LogBasePath + GlobalValue.Config.OperateLogPath;
                fileDownloadName = "OperateLog.zip";
            }
            else
            {
                basePath = GlobalValue.Config.LogBasePath + GlobalValue.Config.InfoLogPath;
                fileDownloadName = "InfoLog.zip";
            }
            string baseDirPath = MyEnvironment.RootPath(basePath);
            if (!Directory.Exists(baseDirPath))
            {
                return null;
            }
            try
            {
                //压缩
                ZipHelper.PackFiles(filePath, baseDirPath);
                byte[] bytes = FileUtil.FileToBytes(filePath);
                FileUtil.Delete(filePath);
                return File(bytes, "application/zip", fileDownloadName);
            }
            catch
            {
                return null;
            }
        }
    }
}
